package com.ai.bitcoin.helper;

import org.springframework.core.task.AsyncTaskExecutor;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;

/**
 * 2017/11/14
 * @author xd
 *
 */

public class ExceptionAsyncTaskExecutor implements AsyncTaskExecutor {
	
	private AsyncTaskExecutor executor;

	//用独立的线程来包装，@Async其本质就是如此  
	@Override
	public void execute(Runnable task) {
		executor.execute(createWrappedRunnable(task));
	}

	@Override
	public void execute(Runnable task, long startTimeout) {
		 executor.execute(createWrappedRunnable(task), startTimeout);
	}

	@Override
	public Future<?> submit(Runnable task) {
		return null;
	}

	@Override
	public <T> Future<T> submit(Callable<T> task) {
		return executor.submit(createCallable(task));
	}
	
	private <T> Callable createCallable(final Callable<T> task) {   
        return new Callable() {   
            public T call() throws Exception {   
                 try {   
                     return task.call();   
                 } catch (Exception ex) {   
                     handle(ex);   
                     throw ex;   
                   }   
                 }   
        };   
    }  
  
    private Runnable createWrappedRunnable(final Runnable task) {   
         return new Runnable() {   
             public void run() {   
                 try {  
                     task.run();   
                  } catch (Exception ex) {   
                     handle(ex);   
                   }   
            }  
        };   
    }   
    
    private void handle(Exception ex) {  
      //具体的异常逻辑处理的地方  
      System.err.println("Error during @Async execution: " + ex);  
    }

	public ExceptionAsyncTaskExecutor(AsyncTaskExecutor executor) {
		super();
		this.executor = executor;
	}

	public ExceptionAsyncTaskExecutor() {
		super();
	}  

    
}
